package com.smfx.yxt.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.model.SchoolCommentWeek;
import com.smfx.yxt.reqVo.WeekCommentQueryVO;
import com.smfx.yxt.service.ISchoolCommentWeekService;
import com.smfx.yxt.service.IStudentWeekCommentService;
import com.smfx.yxt.service.ITermInfoService;
import com.smfx.yxt.view.StudentWeekCommentView;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.utils.TemplateUtil;

@Component
public class WeekCommentJob {
	
	@Autowired
	private ISchoolCommentWeekService schoolCommentWeekService;
	@Autowired
	private IStudentWeekCommentService studentWeekCommentService;
	@Autowired
	private ITermInfoService termInfoService;
	
	/**
	 * 提醒教师填写每周点评
	 */
	@Scheduled(cron = "0 30 7 * * ?")
	public void remaindTeacher() {
		TermView view = termInfoService.selectCurrentTerm();
		WeekCommentQueryVO queryVO = new WeekCommentQueryVO();
		queryVO.setTermId(view.getId());
		queryVO.setFinishYear(view.getFinishYear());
		List<StudentWeekCommentView> list = studentWeekCommentService.selectRemindTeacher(queryVO);
		for(StudentWeekCommentView commentView : list) {
			String reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + MenuConst.WEEK_COMMENT;
			TemplateUtil.comemntRemaindTeacherSend(commentView.getTeacherOpenid(), commentView.getSchoolName(), 
					commentView.getTeacherName(), commentView.getStudentCount() - commentView.getHadStudentCount(), reqUrl);
		}
	}
	
	/**
	 * 每周点评提醒发送给家长
	 */
	@Scheduled(cron = "0 30 17 * * ?")
	public void noticeParent() {
		TermView view = termInfoService.selectCurrentTerm();
		WeekCommentQueryVO queryVO = new WeekCommentQueryVO();
		queryVO.setTermId(view.getId());
		List<SchoolCommentWeek> list = schoolCommentWeekService.selectNoticeParent(queryVO);
		for(SchoolCommentWeek schoolComment : list) {
			queryVO.setSchoolId(schoolComment.getSchoolId());
			queryVO.setWeekNo(schoolComment.getWeekNo());
			List<StudentWeekCommentView> commentList = studentWeekCommentService.selectViewList(queryVO);
			for(StudentWeekCommentView commentView : commentList) {
				String content = commentView.getTeacherCommentContent();
				if(content.length() > 50) {
					content = content.substring(0, 50) + "...";
				}
				String reqUrl = Const.BASEPATH + Const.STUDENT_ROLE + MenuConst.WEEK_COMMENT 
						+ "/detail/" + commentView.getId() + "?sid=" + commentView.getStudentId();
				TemplateUtil.commentNoticeParentSend(commentView.getParentOpenid(), 
						commentView.getStudentName(), content, commentView.getStatus(), reqUrl);
			}
		}
	}
	
}
